<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
class Homedate extends Base{
    public function homedate_list(){
        $keyword=input('get.keyword');
        $state=input('get.state');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['a.time', '>', $fromtime],
            ['a.time', '<', $totime]
        ];
        if($state==0){
            array_push($map,['a.state','<',3]);
        }else{
            array_push($map,['a.state','=',$state]);
        }
        $list=Db::name('homestay_date')->alias('a')->join('homestay b', 'a.h_id = b.id','LEFT')->field('a.*,b.des')->where($map)->where('b.des','like',"%{$keyword}%")->order('a.id desc')->paginate(10);
        $counts=Db::name('homestay_date')->alias('a')->join('homestay b', 'a.h_id = b.id','LEFT')->field('a.*,b.des')->where($map)->where('b.des','like',"%{$keyword}%")->count();
        
        $this->assign('keyword',input('get.keyword'));
        $this->assign('state',$state);
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('counts',$counts);
        return $this->fetch();
    }
    public function homedate_add(){
        if(request()->isGet()){
            $homestay=Db::name('homestay')->where('state',1)->order('id asc')->select();
            $this->assign('date',date('Y-m-d',time()));
            $this->assign('homestay',$homestay);
            $this->assign('domain',$this->request->domain());
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            if(strtotime($param['date'])>=strtotime(date('Y-m-d',time()))){

            }else{
                return json(['code' => 201,'msg' => '可租日期必须自今日起']);
            }
            $data['h_id']=$param['h_id'];
            $data['date']=strtotime($param['date']);
            $data['day_price']=$param['day_price'];
            $data['is_rent']=$param['is_rent'];
            $data['state']=$param['state'];
            $data['time']=time();
            $data['uptime']=time();

            $datef=strtotime(date('Y-m-d',$data['date']));
            $datet=strtotime(date('Y-m-d',$data['date']))+86400;

            $is_day=Db::name('homestay_date')->where('h_id',$data['h_id'])->where('date','>=',$datef)->where('date','<',$datet)->find();
            if($is_day){
                return json(['code' => 202,'msg' => '所选日期已添加']);
            }

            $res=Db::name('homestay_date')->insert($data);
            if($res){
                return json(['code' => 200,'msg' => '成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function homedate_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('homestay_date')->where('id',$param['id'])->find();
            $homestay=Db::name('homestay')->where('state',1)->order('id asc')->select();
            $this->assign('homestay',$homestay);
            $this->assign('data',$data);
            $this->assign('domain',$this->request->domain());
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['day_price']=$param['day_price'];
            $data['state']=$param['state'];
            $data['uptime']=time();
            $res= Db::name('homestay_date')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function homedate_del(){
        $param = input('post.');
        $res= Db::name('homestay_date')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('homestay_date')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('homestay_date')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
}
